package com.doublefish.doublefishrobot.ui.home.s5;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.doublefish.doublefishrobot.R;
import com.doublefish.doublefishrobot.base.BaseActivity;
import com.doublefish.doublefishrobot.ui.home.bean.ModeSettingBean;
import com.doublefish.doublefishrobot.ui.home.widget.ModeSettingItemValuePopView;
import com.doublefish.doublefishrobot.utils.ble.bean.ModeConstant;
import com.doublefish.doublefishrobot.utils.socket.DFS5ProtocolMaker;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 作者:dugaofeng on 2025/1/9 11:52
 */
public class S5ModeRandomListActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView titleTV;

    //循环配置
    private ModeSettingBean.ModeSettingLoopBean loopBean = ModeSettingBean.ModeSettingLoopBean.defaultLoop(ModeConstant.ProtocolTypeS5);

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mode_random_list_s5;
    }

    @Override
    protected void initView() {
        titleTV.setText(R.string.random_mode);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
    }

    @OnClick(R.id.ll_back)
    public void onBackClick() {
        finish();
    }

    @OnClick({R.id.tv_random_all, R.id.tv_random_left, R.id.tv_random_right})
    public void onBtnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_random_all: {
                prepareTrain(1);
            }
            break;
            case R.id.tv_random_left: {
                prepareTrain(2);
            }
            break;
            case R.id.tv_random_right: {
                prepareTrain(3);
            }
            break;
        }
    }

    //准备训练 选择训练时间/次数
    private void prepareTrain(int modeIndex) {
        List<ModeSettingBean.ModeSettingItemBean> itemBeans = new ArrayList<>();
        itemBeans.add(loopBean.getLoopCountItemBean());
        itemBeans.add(loopBean.getLoopTimeItemBean());

        List<String> itemMsgs = new ArrayList<>();
        itemMsgs.add(loopBean.getLoopCountItemBean().getDescWithItemTag(ModeSettingBean.ModeSettingItemTag.ModeSettingItemTagLoopCount));
        itemMsgs.add(loopBean.getLoopTimeItemBean().getDescWithItemTag(ModeSettingBean.ModeSettingItemTag.ModeSettingItemTagLoopTime));

        int index = this.loopBean.getLoopType();
        ModeSettingItemValuePopView popView = new ModeSettingItemValuePopView(this, itemBeans, itemMsgs, index, new ModeSettingItemValuePopView.OnSettingItemValuePopViewListner() {
            @Override
            public void onValueFinish(ModeSettingBean.ModeSettingItemBean itemBean, int index1) {
                loopBean.setLoopType(index1);
                startRandomBall(modeIndex);
            }

            @Override
            public void onValueError(String error) {
                showToast(error);
            }

            @Override
            public void onCancelBtnClick() {

            }
        });
        popView.show();
    }

    private void startRandomBall(int modeIndex) {
        //1.全台随机 2左半台随机 3右半台随机
        String modeName = getString(R.string.random_all);
        if (modeIndex == 2) {
            modeName = getString(R.string.random_left);
        } else if (modeIndex == 3){
            modeName = getString(R.string.random_right);
        }

        //运行的时间 次数
        boolean timeOrCount = loopBean.getLoopType() == 1;

        //循环次数
        int loopCount = timeOrCount ? 0 : loopBean.getLoopCountItemBean().getCurrentValue();
        //发球个数
        int count = loopCount;

        //单位分钟
        int loopMinute = timeOrCount ? loopBean.getLoopTimeItemBean().getCurrentValue() : 0;
        //转化为秒
        int time = loopMinute * 60;

        char[] data = DFS5ProtocolMaker.startRandomMode(modeIndex);
        Intent intent = S5ModeBallActivity.makeIntent(this, modeName, timeOrCount, time, count,  data);
        startActivity(intent);
    }


}
